﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com> :
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// Aero.cs:
//
// Classe que controla funções de transparência de uma janela
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace WBFSSync.AeroControls
{
    class Aero
    {
        //---------------- Estruturas úteis
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;

            public MARGINS(int l, int r, int t, int b)
            {
                Left = l;
                Right = r;
                Top = t;
                Bottom = b;
            }
        }
        
        //---------------- Variáveis

        public static bool IsAeroEnabled { get { try { return DwmIsCompositionEnabled(); } catch { return false; } } }
        public bool Active { get { return active; } set { active = value; } }

        Brush clientBrush;
        Rectangle clientArea;
        MARGINS margins;
        Boolean active = true;

        //---------------- Rotinas

        public Aero(Form window)
        {
            if (IsAeroEnabled)
            {
                margins = new MARGINS(window.Padding.Left, window.Padding.Right,
                    window.Padding.Top, window.Padding.Bottom);

                clientBrush = new SolidBrush(window.BackColor);
                clientArea = new Rectangle(margins.Left, margins.Top,
                    window.ClientRectangle.Width - margins.Left - margins.Right,
                            window.ClientRectangle.Height - margins.Top - margins.Bottom);

                DwmExtendFrameIntoClientArea(window.Handle, ref margins);

                window.Paint += new PaintEventHandler(window_Paint);
                window.Resize += new EventHandler(window_Resize);
                window.BackColorChanged += new EventHandler(window_BackColorChanged);
                window.PaddingChanged += new EventHandler(window_PaddingChanged);
            }
        }

        void window_PaddingChanged(object sender, EventArgs e)
        {
            Form window = (sender as Form);
            margins = new MARGINS(window.Padding.Left, window.Padding.Right,
                    window.Padding.Top, window.Padding.Bottom);
        }

        void window_BackColorChanged(object sender, EventArgs e)
        {
            clientBrush = new SolidBrush((sender as Form).BackColor);
        }

        void window_Resize(object sender, EventArgs e)
        {
            Form window = (sender as Form);
            clientArea = new Rectangle(margins.Left, margins.Top,
                    window.ClientRectangle.Width - margins.Left - margins.Right,
                    window.ClientRectangle.Height - margins.Top - margins.Bottom);
        }

        void window_Paint(object sender, PaintEventArgs e)
        {
            if (IsAeroEnabled)
            {
                e.Graphics.Clear(Color.Black);
                e.Graphics.FillRectangle(clientBrush, clientArea);
            }
        }

        //---------------- Rotinas nativas
        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern int DwmExtendFrameIntoClientArea
                        (IntPtr hwnd, ref MARGINS margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern bool DwmIsCompositionEnabled();
    }
}
